package jangdolbaeng2.seller.dao;

import jangdolbaeng2.db.MybatisConfig;
import jangdolbaeng2.vo.ProductVO;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/*
 * 작성자 : 진승민
 * 기능 : 상품 정보를 조회 수정 삭제하기 위한 DAO 클래스*/
public class ProductManagerDAO {
	SqlSessionFactory factory = MybatisConfig.getSqlSessionFactory();

	// 상품리스트 조회
	public List<ProductVO> list(int shop_seq) {
		List<ProductVO> list = null;
		SqlSession ss= factory.openSession(); 
		try {
			list = ss.selectList("productManager.listProduct", shop_seq);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return list;
	}

	// 상품정보 입력
	public void addProduct(ProductVO vo) {
		SqlSession ss= factory.openSession();
		try {
			ss.insert("productManager.insertProduct",vo);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}

	}

	// 상품정보 수정
	public int updateProduct(ProductVO vo) {
		SqlSession ss= factory.openSession();
		int rs = 0;
		try {
			rs =ss.update("productManager.updateProduct", vo);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		
		return rs;

	}

	// 삭품정보 삭제
	public void deleteProduct(Integer vo) {
		SqlSession ss= factory.openSession();
		try {
			ss.delete("productManager.deleteProduct", vo);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
	}
	
	// 상품 상세 조회
	public ProductVO getOneProduct(int product_seq) {
		SqlSession ss= factory.openSession();
		ProductVO oneProduct = null;
		try {
			oneProduct = ss.selectOne("productManager.getOneProduct", product_seq);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}	
		finally {
			if (ss != null) ss.close();
		}
		return oneProduct;		
	}
	

}
